package comi.example.xiong.bearfruit;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;


import com.example.softdk.ycshop.R;
import java.util.ArrayList;
import java.util.List;

import comi.example.xiong.bearfruit.base.BaseActivity;
import comi.example.xiong.bearfruit.cart.CartFragment;
import comi.example.xiong.bearfruit.home.HomeFragment;
import comi.example.xiong.bearfruit.my.MyFragment;
import comi.example.xiong.bearfruit.type.TypeFragment;

import static android.app.ProgressDialog.show;

public class MainActivity2 extends BaseActivity implements View.OnClickListener {
    private ViewPager viewPager;//用来左右滑动的控件
    private ImageView homeImageView, typeImageView, cartImageView, myImageView;
    private LinearLayout llHome, llType, llCart, llMy;

    private List<Fragment> fragmentList;//用来存放添加到ViewPage中的Fragment的集合




    @Override
    public int initLayout() {
        //EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        return R.layout.activity_main2;
    }

    @Override
    public void initView() {
        viewPager = findViewById(R.id.vp_my);//找到布局中对应的ViewPage
        homeImageView = findViewById(R.id.iv_home);//找到布局中对应的ImageView
        typeImageView = findViewById(R.id.iv_type);//找到布局中对应的ImageView
        cartImageView = findViewById(R.id.iv_cart);//找到布局中对应的ImageView
        myImageView = findViewById(R.id.iv_my);//找到布局中对应的ImageView

        llHome = findViewById(R.id.ll_home);
        llType = findViewById(R.id.ll_type);
        llCart = findViewById(R.id.ll_cart);
        llMy = findViewById(R.id.ll_my);
        //设置点击事件监听
        llHome.setOnClickListener(this);
        llType.setOnClickListener(this);
        llCart.setOnClickListener(this);
        llMy.setOnClickListener(this);


        fragmentList = new ArrayList<>();//初始化集合
        //获得对应的Fragment对象
        HomeFragment homeFragment = new HomeFragment();
        TypeFragment typeFragment = new TypeFragment();
        CartFragment cartFragment = new CartFragment();
        //将对象装到集合中
        MyFragment myFragment = new MyFragment();
        fragmentList.add(homeFragment);
        fragmentList.add(typeFragment);
        fragmentList.add(cartFragment);
        fragmentList.add(myFragment);
        //给ViewPage设置Fragment，（让能滑动切换不同的Fragment）
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        });
        //viewpage滑动监听（能让我们知道滑动到了哪一项）
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                setImageView(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    /**
     * 设置当某一项被选中时的逻辑
     *
     * @param i 选中的那一项的下标
     */
    private void setImageView(int i) {
        resetAllImageView();
        switch (i) {
            case 0:
                homeImageView.setImageResource(R.drawable.home_selected);
                break;
            case 1:
                typeImageView.setImageResource(R.drawable.type_selected);
                break;
            case 2:
                cartImageView.setImageResource(R.drawable.cart_selected);
                break;
            case 3:
                myImageView.setImageResource(R.drawable.my_selected);
                break;
        }
    }

    //将所有ImageView的图片设为灰色
    private void resetAllImageView() {
        homeImageView.setImageResource(R.drawable.home_default);
        typeImageView.setImageResource(R.drawable.type_default);
        cartImageView.setImageResource(R.drawable.cart_default);
        myImageView.setImageResource(R.drawable.my_default);
    }

    @Override
    public void initData() {

    }

    //点击事件发生之后，对于设置了监听的控件，执行该方法
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_home:
                selectedItem(0);
                break;
            case R.id.ll_type:
                selectedItem(1);
                break;
            case R.id.ll_cart:
                selectedItem(2);
                break;
            case R.id.ll_my:
                selectedItem(3);
                break;
        }
    }

    public ViewPager getViewPager() {
        return viewPager;
    }

    private void selectedItem(int i) {
        setImageView(i);//处理图标的变化
        viewPager.setCurrentItem(i);//处理页面的变化
    }
    /*public void goLoginActivity(View view) {
        startActivity(new Intent(this, LoginActivity.class));
    }*/
}
